﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using JYB.SummitExam.Model;
using JYB.SummitExam.BLL;
namespace JYB.SummitExam.UI.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        UserBLL bll = new UserBLL();
        JurisdictionBLL jbll = new JurisdictionBLL();
        public ActionResult Index()
        {
            var name = Request["Name"];
            var pwd = Request["Password"];
            var YZ = Request["yan"];
            var SessionYZ = Session["CheckCode"];
            var Identity = Request["Identity"];
            if (YZ.ToUpper() == SessionYZ.ToString().ToUpper())
            {
                if (Identity == "管理员")
                {
                    var user = bll.Login<SystemAuthorityAdministrator>(name, pwd);
                    if (user != null && user.ID != 0)
                    {
                        var result = jbll.GetJurisdictionList<AuthorityRolePermisson>(user.RoleId);
                        Session["admin"] = result;
                        Session["adminID"] = user.ID;
                        ViewBag.JurisdictionList = result;
                        return View();
                    }
                    else
                    {
                        return Content("<script>alert('登录失败！');location.href='/Login/login'</script>");
                    }
                }
                if (Identity == "教师")
                {
                    var user = bll.Login<SystemTeacher>(name, pwd);
                    if (user != null && user.ID != 0)
                    {
                        var result = jbll.GetJurisdictionList<AuthorityRolePermisson>(user.RoleId);
                        Session["teacher"] = result;
                        ViewBag.JurisdictionList = result;
                        return View();
                    }
                    else
                    {
                        return Content("<script>alert('登录失败！');location.href='/Login/login'</script>");
                    }
                }
                if (Identity == "学生")
                {
                    var user = bll.Login<SystemStudents>(name, pwd);
                    if (user != null && user.ID != 0)
                    {
                        var result = jbll.GetJurisdictionList<AuthorityRolePermisson>(user.RoleId);
                        Session["studentID"] = user.ID;
                        ViewBag.JurisdictionList = result;
                        return View();
                    }
                    else
                    {
                        return Content("<script>alert('登录失败！');location.href='/Login/login'</script>");
                    }
                }
            }
            return Content("<script>alert('验证码有误！');location.href='/Login/login'</script>");
        }
        /// <summary>
        /// 初始化登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 初始化首页
        /// </summary>
        /// <returns></returns>
        public ActionResult HomePage()
        {
            return View();
        }





    }

}
